package cn.tsvtc.health.controller;


import cn.tsvtc.health.common.constant.MessageConstant;
import cn.tsvtc.health.common.entity.CommonResult;
import cn.tsvtc.health.common.entity.PageResult;
import cn.tsvtc.health.common.entity.QueryPageBean;
import cn.tsvtc.health.pojo.CheckGroup;
import cn.tsvtc.health.pojo.CheckItem;
import cn.tsvtc.health.service.CheckGroupService;
import cn.tsvtc.health.service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Athena
 * @date: 2020/11/16 16:30
 * @description:
 */
@RequestMapping("checkgroup")
@RestController
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("page/list")
    @ResponseBody
    public CommonResult getAll(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = checkGroupService.findAll(queryPageBean);
            return new CommonResult(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.COMMON_ERROR);
        }
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommonResult pageList(@PathVariable Integer id){
        try{
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new CommonResult(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkGroup);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.COMMON_ERROR);
        }
    }

    @GetMapping("del/{id}")
    @ResponseBody
    public CommonResult del(@PathVariable Integer id){
        try {
            int delete = checkGroupService.delete(id);
            return new CommonResult(true, MessageConstant.DELETE_CHECKITEM_SUCCESS, delete);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.DELETE_CHECKITEM_FAIL, e.getMessage());
        }
    }

    @PostMapping("update")
    @ResponseBody
    public CommonResult update(@RequestBody CheckGroup checkGroup){

        int update = 0;
        try {
            update = checkGroupService.update(checkGroup);
            return new CommonResult(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, update);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(false, MessageConstant.EDIT_CHECKITEM_FAIL, e.getMessage());
        }
    }

    @PostMapping("insert")
    @ResponseBody
    public CommonResult create(@RequestBody CheckGroup checkGroup){
        int insert = 0;
        try {
            insert = checkGroupService.insert(checkGroup);
            return new CommonResult(true, MessageConstant.ADD_CHECKITEM_SUCCESS, insert);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(false, MessageConstant.ADD_CHECKITEM_FAIL, e.getMessage());
        }
    }

}
